<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>
        <c:choose>
            <c:when test="${user==null}">新增页面</c:when>
            <c:otherwise>编辑页面</c:otherwise>
        </c:choose>
    </title>
    <script src="/res/jquery-3.6.0.min.js"></script>
</head>
<body>
***********
<c:choose>
    <c:when test="${user==null}">新增</c:when>
    <c:otherwise>编辑${user.userName}</c:otherwise>
</c:choose>
************<br/>
<input type="hidden" id="id" value="${user!=null?user.id:''}">
用户名：<input type="text" id="userName" value="${user!=null?user.userName:''}"><br/>
密码：<input type="text" id="password" value="${user!=null?user.password:''}"><br/>
<button id="doSave">保存</button>

</body>

<script>
    jQuery(function(){
        $("#doSave").click(function(){
            var id = $("#id").val()
            var userName = $("#userName").val()
            var password = $("#password").val()

            var data = {}
            data['id'] = id
            data['userName'] = userName
            data['password'] = password
            console.log('data-->'+data)

            $.ajax({
                type:'post',
                url:'/user/save',
                data:JSON.stringify(data),//提交还是文本/字符串 data是json对象 转换成文本
                contentType:'application/json',//发送到服务器的类型
                success:function(ret){
                    if(ret){
                        window.location.href='/home'
                    }else{
                        alert('保存失败.')
                    }
                }
            })

        })
    })
</script>

</html>
